package moxl.parser.assemblers.pi;

import java.util.EnumMap;
import java.util.Map;

import moxl.parser.ParserException;
import moxl.parser.assemblers.AbstractAssemblerContext;
import moxl.tokenizer.Token;
import moxl.tokenizer.TokenStream;

public class ProcessingInstructionAssemblerContext extends AbstractAssemblerContext<ProcessingInstructionAssemblerState>
{
	private final Map<ProcessingInstructionBookmark, Token> bookmarks = new EnumMap<ProcessingInstructionBookmark, Token>(ProcessingInstructionBookmark.class);

	public ProcessingInstructionAssemblerContext(TokenStream tstream)
	{
		super(tstream, ProcessingInstructionAssemblerState.START);
	}

	@Override
	public boolean isNotFinished()
	{
		return getState() != ProcessingInstructionAssemblerState.END;
	}

	// TODO: Promote these bookmark methods to a superclass.
	public Token getBookmarkedToken(ProcessingInstructionBookmark bookmark) throws ParserException
	{
		Token result = bookmarks.get(bookmark);
		if (result == null)
		{
			throw new ParserException("Badly formed processing instruction, there is no target", getLocation());
		}
		return result;
	}

	public void bookmarkToken(ProcessingInstructionBookmark bookmark)
	{
		bookmarks.put(bookmark, getToken());
	}

}
